//
//  LocalStorage.swift
//  suda
//
//  Created by youai on 2019/7/18.
//  Copyright © 2019年 youi. All rights reserved.
//

import Foundation
import SwiftyUserDefaults

class LocalStorage {
    
    static func exists(forKey:String)->Bool{
        return UserDefaults.standard.value(forKey: forKey) != nil
    }
    
    static func remove(forKey:String){
        if exists(forKey: forKey){
            UserDefaults.standard.removeObject(forKey: forKey)
        }
    }
    
    static func getString(forKey:String)->String? {
        let token = UserDefaults.standard.value(forKey: forKey) as? String
        return token
    }

    static func saveString(_ value:String?,forKey:String){
        UserDefaults.standard.setValue(value, forKey: forKey)
    }
    
    static func getBool(forKey:String)->Bool? {
        let token = UserDefaults.standard.value(forKey: forKey) as? Bool
        return token
    }
    
    static func saveBool(_ value:Bool?,forKey:String){
        UserDefaults.standard.setValue(value, forKey: forKey)
    }
    
    static func getInt(forKey:String)->Int? {
        let token = UserDefaults.standard.value(forKey: forKey) as? Int
        return token
    }
    
    static func saveInt(_ value:Int?,forKey:String){
        UserDefaults.standard.setValue(value, forKey: forKey)
    }
    
    static func getDouble(forKey:String)->Double? {
        let token = UserDefaults.standard.value(forKey: forKey) as? Double
        return token
    }
    
    static func saveDouble(_ value:Double?,forKey:String){
        UserDefaults.standard.setValue(value, forKey: forKey)
    }
    
    static func saveAry(_ value:[Any?],forKey:String){
        UserDefaults.standard.setValue(value, forKey: forKey)
    }
    
    static func getAry(forKey:String)->[Any?]? {
        let token = UserDefaults.standard.value(forKey: forKey) as? [Any?]
        return token
    }
    
    static func getAryToZone(forKey:String)->[ZoneCodeModel]? {
        var token = [[String:Any?]]()
        switch forKey {
        case ZoneCode104:
            token = Defaults[\.ZoneCode104Key]
        case ZoneCode105:
            token = Defaults[\.ZoneCode105Key]
        case ZoneCode106:
            token = Defaults[\.ZoneCode106Key]
        case ZoneCode117:
            token = Defaults[\.ZoneCode117Key]
        default: break
        }
        var ary = [ZoneCodeModel]()
        for item in token {
            ary.append(ZoneCodeModel(fromDictionary: item))
        }
        return ary
    }
    
    static func saveDic(_ value:[String:Any?],forKey:String){
        var v = value
        for i in value.keys{
            if let k = value[i],k == nil {
                v.updateValue("", forKey: i)
            }
        }
        UserDefaults.standard.setValue(v, forKey: forKey)
    }
    
    static func getDic(forKey:String)->[String:Any?]? {
        let token = UserDefaults.standard.value(forKey: forKey) as? [String:Any?]
        return token
    }
    
}

extension DefaultsKeys{
    var ZoneCode104Key : DefaultsKey<[[String:Any?]]>  {.init(ZoneCode104, defaultValue: [["code":"104000","name":"全部","sort":""],
                                                                                              ["code":"104001","name":"待支付","sort":""],
                                                                                              ["code":"104002","name":"待发货","sort":""],
                                                                                              ["code":"104003","name":"待收货","sort":""],
                                                                                              ["code":"104004","name":"已收货","sort":""]])
    }
    
    var ZoneCode105Key : DefaultsKey<[[String:Any?]]> {.init(ZoneCode105, defaultValue: [["code":"105001","name":"自营","sort":""],
                                                                                              ["code":"105002","name":"京东配送","sort":""]])
    }
    
    var ZoneCode106Key : DefaultsKey<[[String:Any?]]> {.init(ZoneCode106, defaultValue: [["code":"106001","name":"升序","sort":""],
                                                                                              ["code":"106002","name":"降序","sort":""]])
    }
    
    var ZoneCode117Key : DefaultsKey<[[String:Any?]]> {.init(ZoneCode117, defaultValue: [["code":"117002","name":"积分值","sort":""],
                                                                                              ["code":"117003","name":"兑换量","sort":""]])
    }
}
